<template>
    <div class="mallWallet">
        <van-nav-bar title="金积分" left-arrow @click-left="onClickLeft" />
        <div class="card">
            <span>报单积分余额</span>
            <span>￥{{ userInfo.hasscore }}</span>
            <div class="cardBtm">
                <div>
                    <span>累计充入(积分)</span>
                    <span>{{ PointRecharge }}</span>
                </div>
                <div>
                    <span>累计消费(积分)</span>
                    <span>{{ pointConsumption }}</span>
                </div>
            </div>
        </div>
        <div class="title">
            <div>流水明细</div>
            <van-button @click="routerTo('rechargepoint')">充值积分</van-button>
        </div>
        <div class="list">

            <van-empty description="暂无数据" style="height: 100%;" v-if="flowingList.length == 0" />
            <div class="listItem" v-for="item in flowingList" :key="item.id">
                <div class="left">
                    <span>{{ item.change_type }}</span>
                    <span>{{ item.record_type == 0 ? '消费了' : '返回了' }}{{ item.money }}</span>
                </div>
                <div class="right">
                    <span>{{ item.record_type === 0 ? '-' : '+' }}{{ item.money }}</span>
                    <span>{{ item.createtime }}</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts">
import { onMounted, ref, reactive } from 'vue'
import { useRouter } from 'vue-router';
import { flowing } from '../../../api/index';
import { Toast } from 'vant';

export default {
    name: "orderPoint",
    setup() {
        const router = useRouter();
        const userInfo = JSON.parse(window.localStorage.getItem("userInfo") as string)
        const cartTotalPrice = JSON.parse(window.localStorage.getItem("cartTotalPrice") as string)

        //  累计充值积分
        const page_size = ref('')
        const moneytype = ref('')
        // const flowingList = ref<any>([])
        const total = ref(0)
        const PointRecharge = ref(0)   //积分充值
        const pointConsumption = ref(0)   //积分消费
        const flowingList = ref<any>([])
        const getflowList = () => {
            let formData = new FormData();
            formData.append("type", (page_size.value, moneytype.value))
            flowing(formData).then((res: any) => {
                // console.log(res.data.list.total);
                if (res.code == 1) {
                    flowingList.value = res.data.list.data;
                    res.data.list.data.forEach((item: any) => {
                        if (item.record_type == 0) {
                            pointConsumption.value += (item.money * 1)
                        } else {
                            PointRecharge.value += (item.money * 1)
                        }
                    });
                    total.value = res.data.list.total
                } else {
                    Toast(res.msg);
                }
            });
        };


        // 定义onClickLeft方法
        const onClickLeft = () => history.back();
        const routerTo = (str: string) => router.push('/mine/' + str.toLowerCase())

        // 如果需要在组件挂载后执行某些操作，可以使用onMounted
        onMounted(() => {
            // 组件挂载后的逻辑
            getflowList()
        });

        return {
            onClickLeft, userInfo, routerTo, cartTotalPrice, flowingList,PointRecharge,pointConsumption
        };
    }
}
</script>

<style scoped lang="less">
@import url(../../../style/common.css);

.mallWallet {
    background: #F6F7F9;
    width: 100%;
    height: 100vh;

    .van-nav-bar {
        height: 52px;
    }

    .card {
        width: 345px;
        height: 158px;
        background-image: url('../../../assets/my/explosivw.png');
        background-size: 100% 158px;
        background-repeat: no-repeat;
        display: flex;
        justify-content: space-around;
        align-items: flex-start;
        flex-direction: column;
        padding: 0 26px;
        box-sizing: border-box;
        margin: 28px 15px;

        span {
            font-size: 12px;
            color: #FFFFFF;
            font-weight: 400;

            &:nth-child(1) {
                font-size: 16px;
            }

            &:nth-child(2) {
                font-size: 20px;
            }
        }

        .cardBtm {
            display: flex;
            justify-content: flex-start;

            div {
                display: flex;
                justify-content: center;
                flex-direction: column;
                margin-right: 30px;
            }
        }

    }

    .title {
        font-size: 16px;
        color: #3D3D3D;
        text-align: left;
        // margin-left: 15px;
        display: flex;
        justify-content: space-between;
        padding: 15px;

        .van-button {
            width: 80px;
            height: 30px;
            background: #232323;
            border-radius: 623px 623px 623px 623px;
            font-size: 15px;
            color: #FFFFFF;
            white-space: nowrap;
        }
    }

    .list {
        .listItem {
            margin: 16px 15px 0;
            height: 80px;
            background: #FFFFFF;
            border-radius: 13px 13px 13px 13px;
            display: flex;
            justify-content: space-between;
            padding: 18px 16px;
            box-sizing: border-box;

            .left,
            .right {
                display: flex;
                justify-content: space-between;
                flex-direction: column;
                text-align: left;

                span {
                    &:nth-child(1) {
                        font-size: 16px;
                        color: #232323;
                    }

                    &:nth-child(2) {
                        font-size: 13px;
                        color: #999999;
                    }
                }
            }

            .right {
                text-align: right;
            }
        }
    }
}

:deep(.van-nav-bar__left .van-badge__wrapper) {
    color: #272635;
}
</style>